package org.wannatrak.android.service;

import android.net.Uri;
import android.database.Cursor;
import android.content.ContentValues;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import org.wannatrak.android.entity.BaseEntity;

import java.util.Date;


/**
 * @Author: Maksim Tulupov maxtulupov@gmail.com
 * @Date: $Date: 2011-06-14 13:32:03 +0000 (Tue, 14 Jun 2011) $
 * @Time: 13:45:12
 * @Version: $Revision: 2 $
 * @Id: $Id: CommonService.java 2 2011-06-14 13:32:03Z maxtulupov@gmail.com $
 */
public class CommonService {

    private Context context;

    public CommonService(Context context) {
        this.context = context;
    }

    public Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }

    public long getCurrentDateInMillis() {
        return System.currentTimeMillis();
    }

     public long isEntityExistInDbById(Uri uri, long id) {
        Cursor cursor = null;
        long result;
        try {
            cursor = context.getContentResolver()
                    .query(uri, new String[]{BaseEntity._ID}, BaseEntity._ID + " = ?", new String[]{String.valueOf(id)}, null);
            boolean b = cursor.moveToFirst();
            result = -1;
            if (b) {
                result = cursor.getLong(cursor.getColumnIndex(BaseEntity._ID));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return result;
    }

    public long insertRecord(Uri uri, ContentValues cv) {
        ContentResolver contentResolver = context.getContentResolver();
        return ContentUris.parseId(contentResolver.insert(uri, cv));
    }
}
